//
//  CategoryIcon.swift
//  KoiAccounting
//
//  Created by 纪洪文 on 22.03.25.
//

import Foundation

struct CategoryIcon : Equatable{
    
    // 图标名称
    var name:String
    // 图标唯一值
    var iconUnified: String
    
    var iconType: CategoryIconType
    
    static func == (lhs:CategoryIcon, rhs:CategoryIcon) -> Bool {
        return lhs.iconUnified == rhs.iconUnified
    }
    
    /**
     * 线性图标存储格式
     */
    func getLinearIconUri(unified: String) -> String {
        return "linearicon://\(unified)"
    }
    
    /**
     * 表情图标存储格式
     */
    func getEmojiIconUri(unified: String) -> String {
        return "emojiicon://\(unified.lowercased())"
    }
    
    func getIconUri() -> String {
        switch iconType {
        case .linearicon:
            return getLinearIconUri(unified: self.iconUnified)
        case .emojiicon:
            return getEmojiIconUri(unified: self.iconUnified)
        }
    }
    
    func getIconAsset() -> String {
        switch iconType {
        case .linearicon:
            return CategoryIcon.getIconAssetUri(iconUrl: getLinearIconUri(unified: self.iconUnified))
        case .emojiicon:
            return CategoryIcon.getIconAssetUri(iconUrl: getEmojiIconUri(unified: self.iconUnified))
        }
    }
    
    /**
     根据类别图标url转换成ios asset 中的类别图标资源名
     
     linearicon://dinner
     emojiicon://dinner
     
     */
    static func getIconAssetUri(iconUrl:String) -> String {
        var iconUrlNew = iconUrl
        if (iconUrl.starts(with: "linear_icon")) {
            iconUrlNew = iconUrl.replacingOccurrences(of: "linear_icon", with: "linearicon")
        } else if (iconUrl.starts(with: "emoji_icon")) {
            iconUrlNew = iconUrl.replacingOccurrences(of: "emoji_icon", with: "emojiicon")
        }
        let url = URL(string: iconUrlNew)
        if let host = url?.host, let path = url?.path {
            let iconUnified = host + path
            switch url?.scheme {
                case "linearicon":
                    return String(format: "linear_%@", iconUnified)
                case "emojiicon":
                return String(format: "emoji_koi_%@", iconUnified.lowercased().replacingOccurrences(of: "-", with: "_"))
                default:
                    return ""
            }
        } else {
            return ""
        }
    }
    
    static func getIconUnified(iconUrl: String) -> String {
        var iconUrlNew = iconUrl
        if (iconUrl.starts(with: "linear_icon")) {
            iconUrlNew = iconUrl.replacingOccurrences(of: "linear_icon", with: "linearicon")
        } else if (iconUrl.starts(with: "emoji_icon")) {
            iconUrlNew = iconUrl.replacingOccurrences(of: "emoji_icon", with: "emojiicon")
        }
        let url = URL(string: iconUrlNew)
        if let host = url?.host, let path = url?.path {
            let iconUnified = host + path
            return iconUnified
        } else {
            return ""
        }
    }
    
    static func getCategoryIconType(iconUrl: String) -> CategoryIconType {
        var iconUrlNew = iconUrl
        if (iconUrl.starts(with: "linear_icon")) {
            iconUrlNew = iconUrl.replacingOccurrences(of: "linear_icon", with: "linearicon")
        } else if (iconUrl.starts(with: "emoji_icon")) {
            iconUrlNew = iconUrl.replacingOccurrences(of: "emoji_icon", with: "emojiicon")
        }
        let url = URL(string: iconUrlNew)
        switch url?.scheme {
            case "linearicon":
                return CategoryIconType.linearicon
            case "emojiicon":
                return CategoryIconType.emojiicon
            default:
                return CategoryIconType.linearicon
        }
    }
}

enum CategoryIconType : String {
    case linearicon , emojiicon
}

extension CategoryIconType {
    
    func typeName() -> String {
        switch self {
        case .linearicon:
            return "线性图标"
        case .emojiicon:
            return "emoji图标"
        }
    }
}
